package com.example.module_home.View;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.module_base.Base.BaseActivity;
import com.example.module_base.Controll.MyToolBar;
import com.example.module_home.Adapter.PersonalCodeAdapter;
import com.example.module_home.DB.DBOperationHelper;
import com.example.module_home.DB.SeenCodeTable;
import com.example.module_home.R;
import com.example.module_home.databinding.HomeActivitySeencodeBinding;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by 陈煜 on 2018/6/1.
 */

public class HomeSeenCodeActivity extends BaseActivity<HomeActivitySeencodeBinding> {
    private Toolbar mToolbar;
    private DBOperationHelper<SeenCodeTable> mOperationHelper;
    private List<SeenCodeTable> mSeenCodes;
    @Override
    public int setLayout() {
        return R.layout.home_activity_seencode;
    }

    @Override
    public void init(HomeActivitySeencodeBinding bingding) {
        setupToolbar();
        querySeenCodes();
        setupSeenCodes();
    }


    private void setupSeenCodes() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        PersonalCodeAdapter codeAdapter = new PersonalCodeAdapter(mSeenCodes);
        bingding.recySeenCode.setLayoutManager(manager);
        bingding.recySeenCode.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        bingding.recySeenCode.setAdapter(codeAdapter);
    }

    private void querySeenCodes() {
        try {
            mOperationHelper = new DBOperationHelper<>(this,SeenCodeTable.class);
            mSeenCodes = mOperationHelper.queryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void setupToolbar() {
        mToolbar = new MyToolBar(this)
                .init()
                .addTitle("查看的文章")
                .addHome(1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        addToolBar(bingding.lineToolbar, mToolbar);
    }
}
